﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace GBPVRLibrary.ControlLibrary
{
    public class SkinFont: IDisposable
    {
        public Brush Brush { get; set; }
        public Font Font { get; set; }
        public Color BorderColor { get; set; }
        public int OutlineWidth { get; set; }

        public SizeF MeasureString(string Text, int? Width)
        {
            using (Image temp = new Bitmap(10, 10))
            {
                using (Graphics g = Graphics.FromImage(temp))
                {
                    if (Width != null)
                        return g.MeasureString(Text, this.Font, Width.Value);
                    return g.MeasureString(Text, this.Font);
                }
            }            
        }

        public void Render(Image Image, Rectangle Location, string Text)
        {
            using (Graphics g = Graphics.FromImage(Image))
            {
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;                
                if (OutlineWidth > 0 && BorderColor != Color.Transparent)
                {
                    Brush border = new SolidBrush(BorderColor);
                    for (int i = 1; i <= OutlineWidth; i++)
                    {
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X - i, Location.Y - i, Location.Width, Location.Height)); // top left
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X - i, Location.Y, Location.Width, Location.Height)); // left
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X, Location.Y - i, Location.Width, Location.Height)); // top
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X - i, Location.Y + i, Location.Width, Location.Height)); // bottom left
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X, Location.Y + i, Location.Width, Location.Height)); // bottom
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X + i, Location.Y + i, Location.Width, Location.Height)); // bottom right
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X + i, Location.Y, Location.Width, Location.Height)); // right
                        g.DrawString(Text, this.Font, border, new Rectangle(Location.X + i, Location.Y - i, Location.Width, Location.Height)); // top right
                    }
                }
                g.DrawString(Text, this.Font, this.Brush, Location);
            }
        }

        public void Dispose()
        {
            if (Font != null)
                Font.Dispose();
            Font = null;
            if(Brush != null)
                Brush.Dispose();
            Brush = null;            
        }
    }
}
